package com.kinyx.framework.db.sentence.str.demo.update;

import org.junit.Assert;
import org.junit.Test;

import com.kinyx.framework.db.sentence.str.SQLBuilder;
import com.kinyx.framework.db.sentence.str.SQL;
import com.kinyx.framework.db.sentence.str.UPDATE;

public class UPDATE_Tester {

	final UPDATE update = SQLBuilder.update("T_DEMO");

	String defaultSentenceSQL = "UPDATE T_DEMO";

	protected void test(final UPDATE update, final String expectedSentence, final String expectedString) {
		Assert.assertEquals(expectedSentence, update.getSentence());
		Assert.assertEquals(expectedString, update.toString());
	}

	String defaultStringSQL = "SQL: [" + this.defaultSentenceSQL + "], params: []";

	@Test
	public void test0() {
		this.test(this.update, this.defaultSentenceSQL, this.defaultStringSQL);
	}

	@Test
	public void demo1() {
		final String sentence = "UPDATE T_DEMO SET ID = ? WHERE ID = ?";
		final String str = "SQL: [" + sentence + "], params: [foo, bar]";
		this.test(//
				SQLBuilder.update("T_DEMO")//
						.set("ID", "foo")//
						.where(//
								new SQL().x("ID = ?", "bar")//
						)//
				, sentence, str);
	}

	@Test
	public void demo2() {
		final String sentence = "UPDATE T_DEMO SET ID = ? WHERE (ID = ?)";
		final String str = "SQL: [" + sentence + "], params: [foo, bar]";
		this.test(//
				SQLBuilder.update("T_DEMO")//
						.set("ID", "foo")//
						.where(//
								new SQL().x("(ID = ?)", "bar")//
						)//
				, sentence, str);
	}

	@Test
	public void test_set1() {
		final String sentence = this.defaultSentenceSQL + " SET ID = ?";
		final String str = "SQL: [" + sentence + "], params: [foo]";
		this.test(this.update.set("ID", "foo"), sentence, str);
	}

	@Test
	public void test_set2() {
		final String sentence = this.defaultSentenceSQL + " SET ID = ?, USING_STATUS = ?";
		final String str = "SQL: [" + sentence + "], params: [foo, bar]";
		this.test(this.update.set("ID", "foo").set("USING_STATUS", "bar"), sentence, str);
	}

}
